package com.jjwt.demo;

public class Thread3 {
    private static boolean flag1 = true;
    private static boolean flag2 = false;
    private static boolean flag3 = false;

    public static void main(String[] args) {
        final Thread3 o = new Thread3();//创建一个final的本对象用来加锁
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {//循环10次
                while (true){
                    synchronized (o){
                        if(!flag1){//如果第一个标记为false时 等待
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            System.out.println(Thread.currentThread().getName());
                            flag1 = false;
                            flag2 = true;
                            flag3 = false;
                            o.notifyAll();//唤醒所有线程
                            break;
                        }
                    }
                }
            }
        },"A");
//        Thread t4=new Thread(()->{
//            while(true){
//                synchronized (o){
//                    if(!flag1){
//                        try{
//                            o.wait();
//                        }catch(InterruptedException e){
//                            e.printStackTrace();
//                        }
//                    }else{
//                        System.out.println(Thread.currentThread().getName());
//                        flag1=false;
//                        flag2=true;
//                        flag3=false;
//                        o.notifyAll();
//                        break;
//                    }
//                }
//            }
//        },"asa");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {//循环10次
                while (true){
                    synchronized (o){
                        if(!flag2){//如果第一个标记为false时 等待
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            System.out.println(Thread.currentThread().getName());
                            flag1 = false;
                            flag2 = false;
                            flag3 = true;
                            o.notifyAll();//唤醒所有线程
                            break;
                        }
                    }
                }
            }
        },"B");
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {//循环10次
                while (true){
                    synchronized (o){
                        if(!flag3){//如果第一个标记为false时 等待
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            System.out.println(Thread.currentThread().getName());
                            flag1 = true;
                            flag2 = false;
                            flag3 = false;
                            o.notifyAll();//唤醒所有线程
                            break;
                        }
                    }
                }
            }
        },"C");
        t1.start();
        t2.start();
        t3.start();

    }

}
